#ifndef CELL_h__
#define CELL_h__
#pragma once
#include <functional>

template<int Dim>
class Idx{
public:
	typedef int stateType;

	Idx() { memset(_, 0, sizeof(stateType)*Dim); }
	Idx(stateType * p) { memcpy(_, p, sizeof(stateType) * Dim); }
	Idx(const Idx & s) { memcpy(_, s._, sizeof(stateType) * Dim); }
	stateType _[Dim];
	stateType & operator[](const size_t i) { return _[i]; }
	const stateType & operator[](const size_t i) const { return _[i]; }
	operator stateType*() { return _; }
	operator const stateType*() const { return _; }
	bool operator <(const Idx & s) const{
		for (size_t i = 0; i < Dim; i++)
		{
			if (_[i] == s[i]) continue;
			return _[i] < s[i];
		}
		return false;
	}
	bool operator == (const Idx & s) const{
		for (size_t i = 0; i < Dim; i++)
		{
			if (_[i] == s[i]) continue;
			return false;
		}
		return true;
	}
	Idx& operator =(const Idx & s)
	{
		memcpy(_, s._, sizeof(stateType) * Dim);
		return *this;
	}
};

template<int Dim, class State, class _idx = Idx<Dim>>
class cell
{
public:
	typedef std::function<State(cell *)> newStateFun;
	typedef typename _idx _Idx;
	State state;
	_Idx pos;
	
	State _NextState;
	void nextState(const newStateFun & fun)
	{
		_NextState = fun(this);
	}
	void updateState()
	{
		state = _NextState;
	}
};


#endif // CELL_h__
